<?php
/**
 * member.php
 * 
 * This file contains the code for the webpage displaying the user's preferred hyperlinks' headlines.
 */
error_reporting (E_ALL ^ E_NOTICE);
session_start();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];

require_once 'rss_parser.php';
require_once 'interface.php';
webpageDoctype();
print_html_title("Member System - Home");
webpageMetaAndBodyStart();
if ($username == NULL && $userid == NULL) {
	echo "<p>Please login to access this page. <a href='./login.php'>Login here</a>";
}
else {
	require_once 'connect.php';
	$result = mysql_query("SELECT preferences FROM users WHERE username = '$username'");
	$row = mysql_fetch_array($result);
	$prefString = $row["preferences"];
	
	if (strlen($prefString) == 15) {
		$prefArray = parse_preferences($prefString);
		echo "<div id=\"sidebar\">";
		if ($prefArray[0] == 1 || $prefArray[1] == 1 || $prefArray[2] == 1) {
			echo <<<EOL
		<a href="#news" class="sidebar">News</a><br/><br/>
EOL;
		}
		if ($prefArray[3] == 1 || $prefArray[4] == 1 || $prefArray[5] == 1 || $prefArray[6] == 1 || $prefArray[7] == 1 || 
			$prefArray[8] == 1 || $prefArray[9] == 1 || $prefArray[10] == 1 || $prefArray[11] == 1) {
			echo <<<EOL
		<a href="#tech" class="sidebar">Tech</a><br/><br/>
EOL;
		}
		if ($prefArray[12] == 1 || $prefArray[13] == 1 || $prefArray[14] == 1) {
			echo <<<EOL
		<a href="#sports" class="sidebar">Sports</a><br/><br/>
EOL;
		}
		echo <<<EOL
		</div>
				<div id="content">
					<div id="mainContent">
EOL;
		if ($prefArray[0] == 1 || $prefArray[1] == 1 || $prefArray[2] == 1) {
			echo <<<EOL
		<a name="news"></a><h1>NEWS</h1>
		<hr/>
EOL;
			if ($prefArray[0] == 1) {
				print_parsed_feed(parse_cnn_news_feed());
			}
			if ($prefArray[1] == 1) {
				print_parsed_feed(parse_huffington_feed());
			}
			if ($prefArray[2] == 1) {
				print_parsed_feed(parse_yahoo_news_feed());
			}
			echo <<<EOL
			<div align="center"><a href="#top">Back to Top</a></div>
			<hr/>
EOL;
		}
		if ($prefArray[3] == 1 || $prefArray[4] == 1 || $prefArray[5] == 1 || $prefArray[6] == 1 || $prefArray[7] == 1 || 
			$prefArray[8] == 1 || $prefArray[9] == 1 || $prefArray[10] == 1 || $prefArray[11] == 1) {
			echo <<<EOL
		<a name="tech"></a><h1>TECH</h1>
		<hr/>
EOL;
			if ($prefArray[3] == 1) {
				print_parsed_feed(parse_ars_feed());
			}
			if ($prefArray[4] == 1) {
				print_parsed_feed(parse_cnet_feed());
			}
			if ($prefArray[5] == 1) {
				print_parsed_feed(parse_engadget_feed());
			}
			if ($prefArray[6] == 1) {
				print_parsed_feed(parse_gizmodo_feed());
			}
			if ($prefArray[7] == 1) {
				print_parsed_feed(parse_ign_feed());
			}
			if ($prefArray[8] == 1) {
				print_parsed_feed(parse_joystiq_feed());
			}
			if ($prefArray[9] == 1) {
				print_parsed_feed(parse_kotaku_feed());
			}
			if ($prefArray[10] == 1) {
				print_parsed_feed(parse_max_feed());
			}
			if ($prefArray[11] == 1) {
				print_parsed_feed(parse_wired_feed());
			}
			echo <<<EOL
			<div align="center"><a href="#top">Back to Top</a></div>
			<hr/>
EOL;
		}
		if ($prefArray[12] == 1 || $prefArray[13] == 1 || $prefArray[14] == 1) {
			echo <<<EOL
		<a name="sports"></a><h1>SPORTS</h1>
		<hr/>
EOL;
			if ($prefArray[12] == 1) {
				print_parsed_feed(parse_cnn_si_feed());
			}
			if ($prefArray[13] == 1) {
				print_parsed_feed(parse_sportingnews_feed());
			}
			if ($prefArray[14] == 1) {
				print_parsed_feed(parse_yahoo_sports_feed());
			}
			echo <<<EOL
		<div align="center"><a href="#top">Back to Top</a></div>
		<hr/>
EOL;
		}
	}
	else {
		echo <<<EOL
		<div align="center">
		<p>
		<h3>
		To begin using your HyperLinks user account you must select your user preferences in the User Preferences page.<br/>
		Click <a href="userpreference.php">here</a> to go to the User Preferences page. 
		</h3>
		</p>
		</div>
EOL;
	}
	mysql_close($con);
}
echo "</div></p>";
webpageFooter();
?>